package com.axter.libs.photo.display;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.os.Handler;

import com.axter.libs.photo.bean.MediaInfo;
import com.axter.libs.photo.previewphoto.PreviewPhotoIntent;
import com.axter.libs.photo.previewphoto.PreviewPhotoPreviewActivity;
import com.axter.libs.photo.utils.BaseUtils;
import com.axter.libs.photo.utils.OpenCameraActivity;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

/**
 * Created by zhaobo on 2016/10/14.
 */

public class SimpleChoosePhotoCallBack implements IChoosePhotoCallBackInterface {
    private static final int REQUESTCODE_CAMERA = 101;

    @Override
    public void openCamera(Activity activity, String dirPath, int now, int max) {
        Intent intent = new Intent(activity, OpenCameraActivity.class);
        intent.putExtra(OpenCameraActivity.CAMERA_PATH, getCameraPicPath(activity.getApplicationContext()));
        activity.startActivityForResult(intent, REQUESTCODE_CAMERA);
    }

    @Override
    public String onResultActivityByCamera(Handler handler, int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == REQUESTCODE_CAMERA) {
            if (data != null && data.getExtras() != null && data.getExtras().containsKey(OpenCameraActivity.CAMERA_PATH)) {
                return data.getExtras().getString(OpenCameraActivity.CAMERA_PATH);
            }
        }
        return null;
    }

    @Override
    public boolean isDirectFinish() {
        return true;
    }

    @Override
    public boolean isIncrementFinish() {
        return false;
    }

    @Override
    public void openVideo(Activity activity, String videoPath, boolean isChecked) {

    }

    @Override
    public void openPreview(Activity activity, MediaInfo mediaInfo) {
        ArrayList<String> list = new ArrayList<>();
        list.add(mediaInfo.getPath());
        new PreviewPhotoIntent(activity, PreviewPhotoPreviewActivity.EXTRA_TYPE_PREVIEW)
                .setImages(list)
                .setPosition(0)
                .startActivity(activity);
    }

    private String getCameraPicPath(Context context) {
        return BaseUtils.getFilePath(context,
                new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Camera").getAbsolutePath(),
                "MRUN_" + new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()) + ".jpg");
    }
}
